package com.yc.cloud.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.cloud.entity.HwCameraSnapshotRelation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 摄像头抓图策略关联表 Mapper 接口
 * </p>
 *
 * @author kinggu
 * @since 2025-10-10
 */
@Mapper
public interface HwCameraSnapshotRelationMapper extends BaseMapper<HwCameraSnapshotRelation> {

    /**
     * 获取摄像头的有效策略关联
     */
    default HwCameraSnapshotRelation selectEffectiveRelationByCameraId(Long cameraId) {
        LocalDate today = LocalDate.now();
        return this.selectOne(
            new QueryWrapper<HwCameraSnapshotRelation>()
                .eq(HwCameraSnapshotRelation.CameraId, cameraId)
                .eq(HwCameraSnapshotRelation.Deleted, false)
                .and(wrapper -> wrapper
                    .isNull("effective_date")
                    .or()
                    .le("effective_date", today)
                )
                .and(wrapper -> wrapper
                    .isNull("expiry_date")
                    .or()
                    .ge("expiry_date", today)
                )
                .last("LIMIT 1")
        );
    }
    
    /**
     * 获取策略关联的所有摄像头ID
     */
    default List<Long> selectCameraIdsByStrategyId(Long strategyId) {
        return this.selectList(
            new QueryWrapper<HwCameraSnapshotRelation>()
                .eq(HwCameraSnapshotRelation.StrategyId, strategyId)
                .eq(HwCameraSnapshotRelation.Deleted, false)
        ).stream().map(HwCameraSnapshotRelation::getCameraId).toList();
    }
} 